function Slider(selector, defaultIndex = 0) {
    this.target = document.querySelector(selector)
    this.wrapper = this.target.querySelector('ul')
    this.control = document.querySelector('.slider-control .year ul')
    this.controlItem = this.control.querySelectorAll('li')
    this.descItem = document.querySelectorAll('.slider-desc-item')

    if (this.target) {
        this.images = this.wrapper.querySelectorAll('li')
        this.imagesNum = this.images.length
        this.centerIndex = Math.floor(this.imagesNum / 2)

        this.setImageActiveByIndex(defaultIndex)
    } else {
        // eslint-disable-next-line no-undef
        throw error('Slider cannot get DOM')
    }
    if (this.controlItem) {
        let num = this.controlItem.length

        while (num--) {
            this.controlItem[num].setAttribute('data-index', num)
        }
    }
}

Slider.prototype.setImageActiveByIndex = function(index) {
    let num = this.imagesNum

    if (index >= 0 && index < num) {
        this.activeIndex = index
        while (num--) {
            this.images[num].className = ''
            this.controlItem[num].className = ''
            this.descItem[num].className = 'slider-desc-item'
        }
        this.images[index].className = 'cur'
        this.controlItem[index].className = 'cur'
        /* eslint-disable */
        index - 2 >= 0 && (this.images[index - 2].className = 'sibling2');
        index + 2 < this.imagesNum && (this.images[index + 2].className = 'sibling2');
        if (index - 1 >= 0) {
            this.images[index - 1].className = 'sibling';
            this.controlItem[index - 1].className = 'sibling';
        }
        if (index + 1 < this.imagesNum) {
            this.images[index + 1].className = 'sibling';
            this.controlItem[index + 1].className = 'sibling';
        }
        this.wrapper.style = `transform: translate3D(${125*(2-index)}px, 0, 0);`;
        this.control.style = `transform: translate3D(${150*(2-index)}px, 0, 0);`;
        this.descItem[index].className = 'slider-desc-item show';
    } else {
        console.log('setImageByIndex index error');
    }
};

Slider.prototype.next = function() {
    let num = this.imagesNum;

    if (this.activeIndex < num - 1) {
        // this.activeIndex++;
        this.setImageActiveByIndex(this.activeIndex + 1);
    }

};

Slider.prototype.prev = function() {
    if (this.activeIndex > 0) {
        // this.activeIndex--;
        this.setImageActiveByIndex(this.activeIndex - 1);
    }
};

module.exports = Slider;